package com.wlm.controller;

import com.wlm.pojo.Item;
import com.wlm.pojo.ItemDesc;
import com.wlm.service.ItemService;
import com.wlm.vo.EasyUITable;
import com.wlm.vo.SysResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
@RestController
@RequestMapping("/item")
public class ItemController {

    @Resource
    private ItemService itemService;

    @RequestMapping("/query")
    public EasyUITable findItemByPage(Integer page, Integer rows) {
        return itemService.findItemByPage(page, rows);
    }

    @RequestMapping("/save")
    public SysResult saveItem(Item item, ItemDesc itemDesc) {
        try {
            itemService.saveItem(item,itemDesc);
            return SysResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.fail();
        }
    }

    @RequestMapping("/update")
    public SysResult updateItem(Item item) {
        itemService.updateItem(item);
        return SysResult.success();
    }


    @RequestMapping("/delete")
    public SysResult deleteItems(Long[] ids) {
        itemService.deleteItems(ids);
        return SysResult.success();
    }

    @RequestMapping("/instock")
    public SysResult instock(Long[] ids) {
        int status = 2;
        itemService.updateItemState(status,ids);
        return SysResult.success();
    }

    @RequestMapping("/reshelf")
    public SysResult reshelf(Long[] ids) {
        int status = 1;
        itemService.updateItemState(status,ids);
        return SysResult.success();
    }


    @RequestMapping("/query/item/desc/{itemId}")
    public SysResult findItemDescById(@PathVariable Long itemId) {
        ItemDesc itemDesc = itemService.findItemDescById(itemId);

        // 将数据回传给页面
        return SysResult.success(itemDesc);
    }

}
